<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="workplace">
        <el-input
          v-model="queryParams.workplace"
          placeholder="请输入工作单位"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          v-hasPermi="['portal:personnel:query']"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button> -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          v-hasPermi="['portal:personnel:create']"
          @click="handleAdd"
          >录入</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-upload2"
          @click="handleImport"
          >导入</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-download"
          @click="handleExport"
          :loading="exportLoading"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="talentList" style="width: 100%">
      <el-table-column
        label="序号"
        fixed
        align="center"
        type="index"
        width="50"
      >
        <template slot-scope="scope">
          <span>{{
            scope.$index + (queryParams.pageNo - 1) * queryParams.pageSize + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" width="100" />
      <el-table-column label="性别" align="center" prop="configType" width="50">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.SYSTEM_USER_SEX"
            :value="scope.row.gender"
          />
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industry" />
      <el-table-column label="工作单位" align="center" prop="workplace" />
      <el-table-column label="职务" align="center" prop="organizationalRoles" />
      <el-table-column label="职称" align="center" prop="title" />
      <el-table-column
        label="工作时间"
        align="center"
        prop="workTime"
        width="100"
      />
      <el-table-column label="起草标准" align="center" prop="draftingStandard">
        <template v-slot="scope">
          <div class="text-ellipsis" @click="handleDetails(scope.row)">
            {{ scope.row.draftingStandard }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="国防工业领域标准论文"
        align="center"
        prop="publishedPapers"
      >
        <template v-slot="scope">
          <div class="text-ellipsis" @click="handleDetails(scope.row)">
            {{ scope.row.publishedPapers }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="科研项目研究"
        align="center"
        prop="projectResearch"
      >
        <template v-slot="scope">
          <div class="text-ellipsis" @click="handleDetails(scope.row)">
            {{ scope.row.projectResearch }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="编写教材/书籍"
        align="center"
        prop="authoredBooks"
      >
        <template v-slot="scope">
          <div class="text-ellipsis" @click="handleDetails(scope.row)">
            {{ scope.row.authoredBooks }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creatorName" />
      <el-table-column
        label="创建时间"
        width="180"
        align="center"
        prop="createTime"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="240"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-s-order"
            v-hasPermi="['portal:personnel-certificate:query']"
            @click="handleCertificate(scope.row)"
            >证书</el-button
          >
          <el-button
            type="text"
            icon="el-icon-view"
            v-hasPermi="['portal:personnel-certificate:query']"
            @click="handleDetails(scope.row)"
            >查看</el-button
          >
          <el-button
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['portal:personnel:update']"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['portal:personnel:delete']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="725px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        class="talentFrom"
        :disabled="fromDisabled"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="form.gender">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作单位" prop="workplace">
              <el-input v-model="form.workplace" placeholder="请输入工作单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作时间" prop="workTime">
              <el-date-picker
                v-model="form.workTime"
                type="month"
                placeholder="选择工作时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属行业" prop="industry">
              <el-input v-model="form.industry" placeholder="请输入所属行业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="organizationalRoles">
              <el-input
                v-model="form.organizationalRoles"
                placeholder="请输入职务"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="职称" prop="title" class="formItem">
              <el-input
                v-model="form.title"
                placeholder="请输入职称"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="起草标准" prop="draftingStandard">
              <el-input
                type="textarea"
                maxlength="2000"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 10 }"
                v-model="form.draftingStandard"
                placeholder="请输入起草标准情况"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="领域标准论文" prop="publishedPapers">
              <el-input
                type="textarea"
                maxlength="2000"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 10 }"
                v-model="form.publishedPapers"
                placeholder="请输入内容领域标准论文情况"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="科研项目研究" prop="projectResearch">
              <el-input
                type="textarea"
                maxlength="2000"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 10 }"
                v-model="form.projectResearch"
                placeholder="请输入内容科研项目研究情况"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="编写教材/书籍" prop="authoredBooks">
              <el-input
                type="textarea"
                maxlength="2000"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 10 }"
                v-model="form.authoredBooks"
                placeholder="请输入教材/书籍编写情况"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!fromDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <div slot="footer" class="dialog-footer" v-else>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
     <!-- 用户导入对话框 -->
     <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPersonnelList,
  createPersonnel,
  updatePersonnel,
  detailsPersonnel,
  delPersonnel,
  exportPersonnel,
} from "@/api/personnel/talent";
import { DICT_TYPE, getDictDatas } from "@/utils/dict";
import { getAccessToken } from "@/utils/auth";
export default {
  name: "Config",
  dicts: ["sys_user_sex"],
  data() {
    return {
      // 遮罩层
      loading: true,
      //导出遮罩
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 参数表格数据
      talentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: "",
        workplace: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" },
        ],
        workTime: [
          { required: true, message: "工作时间不能为空", trigger: "change" },
        ],
        workplace: [
          { required: true, message: "工作单位不能为空", trigger: "blur" },
        ],
        organizationalRoles: [
          { required: true, message: "职务不能为空", trigger: "blur" },
        ],
        industry: [
          { required: true, message: "所属行业不能为空", trigger: "blur" },
        ],
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getAccessToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/admin-api/portal/personnel/import",
      },
      //性别字典项
      sexDictDatas: getDictDatas(DICT_TYPE.SYSTEM_USER_SEX),
      fromDisabled: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getPersonnelList(this.queryParams).then((res) => {
        this.talentList = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        gender: 1,
        workTime: undefined,
        workplace: undefined,
        organizationalRoles: undefined,
        draftingStandard: undefined,
        publishedPapers: undefined,
        projectResearch: undefined,
        authoredBooks: undefined,
        industry: undefined,
        title: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.fromDisabled = false;
      this.open = true;
      this.title = "人员添加";
    },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.configId)
    //   this.single = selection.length != 1
    //   this.multiple = !selection.length
    // },
    /** 证书按钮操作 */
    handleCertificate(row) {
      this.$router.push("/personnel/talent/certificate/list/" + row.id);
    },
    /** 查看按钮操作 */
    handleDetails(row) {
      detailsPersonnel(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "人员详情";
        this.fromDisabled = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.fromDisabled = false;
      detailsPersonnel(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "人员修改";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        this.form.workTime = this.$moment(this.form.workTime).format(
          "YYYY-MM-DD"
        );
        if (valid) {
          if (this.form.id != undefined) {
            updatePersonnel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            createPersonnel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除"' + row.name + '"的数据项？')
        .then(function () {
          return delPersonnel(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      exportPersonnel().then((response) => {
        this.$download.excel(response, "用户导入模板.xls");
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      if (response.code !== 0) {
        this.$modal.msgError(response.msg);
        return;
      }
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      // 拼接提示语
      // let data = response.data;
      // let text = "创建成功数量：" + data.createUsernames.length;
      // for (const username of data.createUsernames) {
      //   text += "<br />&nbsp;&nbsp;&nbsp;&nbsp;" + username;
      // }
      // text += "<br />更新成功数量：" + data.updateUsernames.length;
      // for (const username of data.updateUsernames) {
      //   text += "<br />&nbsp;&nbsp;&nbsp;&nbsp;" + username;
      // }
      // text +=
      //   "<br />更新失败数量：" + Object.keys(data.failureUsernames).length;
      // for (const username in data.failureUsernames) {
      //   text +=
      //     "<br />&nbsp;&nbsp;&nbsp;&nbsp;" +
      //     username +
      //     "：" +
      //     data.failureUsernames[username];
      // }
      this.$modal.msgSuccess("导入成功");
      this.dangerouslyUseHTMLString= true
      // this.$alert(text, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal
        .confirm("是否确认导出人员数据项?")
        .then(() => {
          // 处理查询参数
          let params = { ...this.queryParams };
          params.pageNo = undefined;
          params.pageSize = undefined;
          this.exportLoading = true;
          return exportPersonnel(params);
        })
        .then((response) => {
          this.$download.excel(response, "人员列表.xls");
        })
        .finally(() => {
          this.exportLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  .el-table {
    font-size: 12px !important;
  }
}
.talentFrom {
  user-select: none;
}
::v-deep .talentFrom .el-input__inner {
  // width: 220px !important;
}
// ::v-deep .talentFrom .el-textarea__inner {
//   width: 580px;
// }
::v-deep .formItem .el-input__inner {
  width: 100% !important;
}
::v-deep .el-input__count {
  color: #909399;
  background: #fff;
  position: absolute;
  font-size: 10px;
  bottom: 1px;
  right: 10px;
  height: 26px;
}
::v-deep .el-tooltip__popper {
  display: none !important;
}
.text-ellipsis {
  // max-height: 100px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #1890ff;
  cursor: pointer;
  &:hover {
    text-decoration: underline;
  }
}
// /* Webkit 浏览器（如 Chrome, Safari） */
// .text-ellipsis::-webkit-scrollbar {
//   width: 8px; /* 滚动条宽度 */
// }

// .text-ellipsis::-webkit-scrollbar-track {
//   background: #f1f1f1; /* 滚动条轨道背景色 */
// }

// .text-ellipsis::-webkit-scrollbar-thumb {
//   background: #888; /* 滚动条滑块背景色 */
//   border-radius: 4px; /* 滑块圆角 */
// }

// .text-ellipsis::-webkit-scrollbar-thumb:hover {
//   background: #555; /* 滑块悬停时的背景色 */
// }

// /* Firefox */
// .text-ellipsis {
//   scrollbar-width: thin; /* 滚动条宽度 */
//   scrollbar-color: #888 #f1f1f1; /* 滑块颜色 轨道颜色 */
// }
</style>
